<?php
/**
 * Base View class that can be extended
 * 
 * @package adroit
 * @subpackage view
 */
class BaseView {
    
    /**
     * @var String Name of Smarty configuration file
     */
    public $configFile;
    
    /**
     * @var String Name of View specific Smarty configuration file section
     */
    public $configSection;

    /**
     * @var Smarty Smarty object
     */
    public $smarty;
    
    /**
     * Constructor
     *
     * @return BaseView
     */
    public function BaseView() {
        $this->configureSmarty();
        $this->configFile = 'template-config.conf';
        $this->configSection = Adroit::$package . Adroit::$controller . Adroit::$action;
    }
    
    /**
     * Entry point into View.
     * Sets data from Model into Smarty object.
     * Loads appropriate Smarty configuration section
     * Displays appropriate Smarty template
     *
     * @param Array $data Data passed in from Model
     */
    public function render($data, $layout) {
        $this->setDataValues($data);
        $this->smarty->assign('WEB_ROOT', WEB_ROOT);
		$this->smarty->assign('CUR_PATH', WEB_ROOT . (Adroit::$package != '' ? Adroit::$package . '/' : ''));
        $this->smarty->config_load($this->configFile, $this->configSection);
        $vars = $this->smarty->get_config_vars();
        $bodyTpl = $this->getBody();
        if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == strtolower('XMLHttpRequest')) {
        	$this->smarty->display($bodyTpl);	
        }else {
        	$this->smarty->assign('bodyTpl', $bodyTpl);
	        $this->smarty->display($layout . '.tpl');
        }
    }
	
	private function getBody() {
		$vars = $this->smarty->get_config_vars();
		if(isset($vars['body'])) {
			$bodyTpl = $vars['body'];
		}else {
			$templatePath = strtolower((Adroit::$package != '' ? Adroit::$package . '/' : '') . Adroit::$controller . '/');
			if(Adroit::$nonActionView != null) {
				$bodyTpl = Adroit::$nonActionView . '.tpl';
			}else {
				$bodyTpl = Adroit::$action . '.tpl';
			}
			$bodyTpl = self::getTemplatePath() . strtolower($bodyTpl);
		}

        if(!file_exists(APP_PATH . 'view/' . $bodyTpl)) {
            $bodyTpl = strtolower(self::getTemplatePath() . 'index.tpl');
        }
		return $bodyTpl;
	}
	
    /**
     * Sets all parameters in $data Array into Smarty object.
     * Uses the same keys from the original Array.
     *
     * @param Array $data
     */
    protected function setDataValues($data) {
        foreach($data AS $key => $value) {
            $this->smarty->assign($key, $value);
        }
    }
    
    /**
     * Instantiates a Smarty object for use in the View.
     * Configures Smarty template/config/cache/compile directories.
     */
    private function configureSmarty() {
        require_once(ADROIT_PATH . 'Smarty/Smarty.class.php');
        $this->smarty = new Smarty();
        $this->smarty->template_dir = APP_PATH . 'view';
        $this->smarty->compile_dir = APP_PATH . 'view/compile_dir';
        $this->smarty->config_dir = APP_PATH . 'view';
		$this->smarty->plugins_dir = array(
            'plugins',
			ADROIT_PATH . 'smarty_plugins',
            APP_PATH . 'smarty_plugins'
        );
    }
	
	public static function getTemplatePath() {
		return strtolower((Adroit::$package != '' ? Adroit::$package . '/' : '') . Adroit::$controller . '/');
	}
    
}
?>